/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Collections;

namespace ELE.EnterpriseLibrary.Messaging
{
  /// <summary>
  /// Configuration settings for messaging.
  /// 
  /// Author : Miguel Curi
  /// 
  /// </summary>
  public class MessageEndPoint
  {
    /// <summary>
    /// Definition for localhost machine name
    /// </summary>
    public const string ENDPOINT_LOCALHOST = @".";
    private string mServerName = ENDPOINT_LOCALHOST;
    private Hashtable mSinks;
    private Hashtable mSources;

    /// <summary>
    /// Default constructor
    /// </summary>
    public MessageEndPoint()
    {
    }
    /// <summary>
    /// The name of the server that contains the MSMQ service running
    /// </summary>
    public string ServerName
    {
      get 
      { 
        return mServerName; 
      }
      set 
      { 
        if(value == null || value.Equals(string.Empty))
        {
          value = ENDPOINT_LOCALHOST;
        }
        mServerName = value; 
        if(mSinks != null)
        {
          MessageSinkInfo[] msis = new MessageSinkInfo[mSinks.Count];
          int i = 0;
          foreach(MessageSinkInfo sink in mSinks.Values)
          {
            sink.ServerName = mServerName;
            msis[i] = sink;
            i++;
          }
          mSinks = null;
          this.Sinks = msis;
        }
        if(mSources != null)
        {
          MessageSourceInfo[] msis = new MessageSourceInfo[mSources.Count];
          int i = 0;
          foreach(MessageSourceInfo source in mSources.Values)
          {
            source.ServerName = mServerName;
            msis[i] = source;
            i++;
          }
          mSources = null;
          this.Sources = msis;
        }
      }
    }
    /// <summary>
    /// Message sinks
    /// </summary>
    
    [XmlArray(ElementName="Sinks")]
    [XmlArrayItem("MessageSinkInfo", typeof(MessageSinkInfo))]
    public MessageSinkInfo[] Sinks
    {
      get 
      { 
        if (mSinks ==  null)
          return null;

        MessageSinkInfo[] sinks = new MessageSinkInfo[mSinks.Values.Count];
        mSinks.Values.CopyTo(sinks,0);
        return sinks;
      }
      set 
      { 
        mSinks = new Hashtable();
        foreach(MessageSinkInfo msi in value)
        {
          msi.ServerName = ServerName;
          mSinks.Add(msi.Id, msi); 
        }
      }
    }
    /// <summary>
    /// Message Sources
    /// </summary>
    
    [XmlArray(ElementName="Sources")]
    [XmlArrayItem("MessageSourceInfo", typeof(MessageSourceInfo))]
    public MessageSourceInfo[] Sources
    {
      get 
      { 
        if (mSources ==  null)
          return null;

        MessageSourceInfo[] sources = new MessageSourceInfo[mSources.Values.Count];
        mSources.Values.CopyTo(sources,0);
        return sources;
      }
      set 
      { 
        mSources = new Hashtable();
        foreach(MessageSourceInfo msi in value)
        {
          msi.ServerName = ServerName;
          mSources.Add(msi.Id, msi); 
        }
      }
    }
    /// <summary>
    /// Get a message sink defined for a particular path
    /// </summary>
    /// <param name="sinkId"></param>
    /// <returns></returns>
    public MessageSinkInfo GetMessageSinkInfo(string sinkId)
    {
      return (MessageSinkInfo)mSinks[sinkId];
    }
    /// <summary>
    /// Get a message source defined for a particular path
    /// </summary>
    /// <param name="sourceId"></param>
    /// <returns></returns>
    public MessageSourceInfo GetMessageSourceInfo(string sourceId)
    {
      return (MessageSourceInfo)mSources[sourceId];
    }
  }
}
